package com.fusion.lottery.auth.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fusion.config.annotation.RequiredRole;
import com.fusion.lottery.auth.service.UserService;
import com.fusion.lottery.entity.auth.User;
import com.fusion.utils.json.Msg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author louis
 *
 */
@Api(description="用户操作接口")
@RestController
@RequestMapping(value = "/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@ApiOperation(value="获取用户列表", notes="获取用户列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "start", value = "起始记录", required = false, dataType = "Long" ,paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Long" ,paramType = "query")
	})
	@GetMapping
	@RequiredRole(value="ADMIN")
	public Msg getUsers(
			@RequestParam(defaultValue = "20", required = false, value = "pageSize") int pageSize,
			@RequestParam(defaultValue = "0", required = false, value = "start") int start,
			User user) {
		return userService.list(user,start,pageSize);
	}
	
	@ApiOperation(value="获取用户", notes="获取用户")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String",paramType = "path")
	@GetMapping("/{id}")
    public Msg getUser(@PathVariable String id) {
        return userService.get(id);
    }
    
	@ApiOperation(value="添加用户", notes="添加用户")
	@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
    @PostMapping
    public Msg save(@Valid @RequestBody User user) {
		return userService.add(user);
    }
    
	@ApiOperation(value="修改用户", notes="修改用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String",paramType = "path"),
		@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "User")
	})
    @PutMapping("/{id}")
    public Msg update(@PathVariable String id, @RequestBody User user) {
		user.setId(id);
		return userService.update(user);
    }
	
	@ApiOperation(value="禁用用户", notes="禁用用户")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String",paramType = "path"),
		@ApiImplicitParam(name = "user", value = "用户实体", required = true, dataType = "UserEntity")
	})
    @PutMapping("/{id}/disabled")
    public Msg disabled(@PathVariable String id, @RequestBody User user) {
		user.setId(id);
		return userService.update(user);
    }
    
	@ApiOperation(value="删除用户", notes="删除用户")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String",paramType = "path")
    @DeleteMapping("/{id}")
    public Msg delete(@PathVariable String id) {
		return userService.delete(id);
    }
    
}